package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.activity.AccountPurchaseHistoryActivity;
import com.qire.manhua.databinding.ActivityAccountPurchaseHistoryBinding;
import com.qire.manhua.databinding.LayoutPurchaseHistoryItemBinding;
import com.qire.manhua.model.viewModel.impl.AccountPurchaseHistoryViewModel;
import com.qire.manhua.model.viewModel.impl.AccountPurchaseHistoryViewModel.PurchaseHistoryItemViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;

public class AccountPurchaseHistoryController extends EventController<AccountPurchaseHistoryActivity> implements AutoVariable<LayoutPurchaseHistoryItemBinding> {

    private AccountPurchaseHistoryViewModel viewModel;

    public AccountPurchaseHistoryController(@NonNull AccountPurchaseHistoryActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityAccountPurchaseHistoryBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setAccountPurchaseHistoryController(this);
    }

    @Override
    public void bindVariable(LayoutPurchaseHistoryItemBinding viewBind) {
        viewBind.setAccountPurchaseHistoryController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(AccountPurchaseHistoryViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout) {
        viewModel.loadMoreData();
    }

    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadData();
    }

    public void backOnClick(View view) {
        owner.onBackPressed();
    }

    public void historyItemOnClick(View view) {
        PurchaseHistoryItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), PurchaseHistoryItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                    .withData("comicBookId", itemViewModel.comicId)
                    .navigation();
        }
    }

}
